import {IN_CREMENT,DE_CREMENT,} from "./actionTypes"
import {LOGIN_START,LOGIN_SUCCESS,LOGIN_FAIL} from "./actionTypes"
import {userLogin} from '../api/user'
import {message} from "antd"

export const increment = (payload) => {
  return {
    type: IN_CREMENT,
    payload
  }
}

export const decrement = (payload) => {
  return {
    type: DE_CREMENT,
    payload
  }
}

export const loginStart=(payload)=>{
  return{
    type:LOGIN_START,
    payload
  }
}

export const loginSuccess=(payload)=>{
  return{
    type:LOGIN_SUCCESS,
    payload
  }
}

export const loginFail=(payload)=>{
  return{
    type:LOGIN_FAIL,
    payload
  }
}

export const loginAysc=(payload,history)=>{ //异步action
  return dispatch=>{ //redux-thunk在此处将dispatch进行拦截
    dispatch(loginStart()) //手动触发dispatch
    userLogin(payload).then(res => {
      dispatch(loginSuccess(res.data))
      message.success("登录成功,进行跳转")
      history.push("/admin")
    }).catch(err=>{
      console.log(err)
      dispatch(loginFail())
      message.error("用户名或密码错误")
    })
  }
}